package com.whoru.logica;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.StringTokenizer;


import android.app.AlertDialog;
import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Matrix;
import android.graphics.drawable.Drawable;

import android.graphics.drawable.BitmapDrawable;




import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import com.whoru.beans.User;

public class Marcadores extends ItemizedOverlay {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	Context mContext;
	User MyUser = new User();

	
	public Marcadores(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
	}
	
	public Marcadores(Drawable defaultMarker, Context context,User u) {
		  super(boundCenterBottom(defaultMarker));
		  mContext = context;
		  MyUser=u;
	
		}
	
	public Marcadores(Drawable defaultMarker, Context context) {
		  super(boundCenterBottom(defaultMarker));
		  mContext = context;
		}
	
	@Override
	public  boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

		 try{
				String url="";
				url= item.getSnippet();
				
				String Imagen,Info;
				StringTokenizer subelementos;
			
				subelementos = new StringTokenizer(url,"*");  
			 
				Imagen = subelementos.nextToken();  
				Info = subelementos.nextToken();
			
				Imagen = "http://62.99.17.90/whoru15/"+Imagen;
		
				Drawable drawable = LoadImageFromWebOperations(Imagen);
				
				drawable = cambiarTamano (drawable);
		
						dialog.setTitle(item.getTitle());
						dialog.setMessage(Info);
						dialog.setIcon(drawable);
						dialog.show();
						
				
		    }catch (Exception e){
		    	
		    	dialog.setTitle(item.getTitle());
				dialog.setMessage(item.getSnippet());
				dialog.show();
		    }

		

	  return true;
	}
	
	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}
	
	public void removeOverlay(int i) {
	    mOverlays.remove(i);
	}
	
	public void clear() {
	    mOverlays.clear();
	}

	@Override
	public int size() {
	  return mOverlays.size();
	}
	
	@Override
	protected OverlayItem createItem(int i) {
	  return mOverlays.get(i);
	}
	
    private Drawable LoadImageFromWebOperations(String url){
    	try{
    	InputStream is = (InputStream) new URL(url).getContent();
    	Drawable d = Drawable.createFromStream(is, "src name");
    	return d;
    	}catch (Exception e) {
    	System.out.println("Exc="+e);
    	return null;
    	}
    	}
    
    
   public Drawable cambiarTamano (Drawable d){
	   
    
       if (d != null) {
           Bitmap bitmapOrg = ((BitmapDrawable) d).getBitmap();
           int width = bitmapOrg.getWidth();
           int height = bitmapOrg.getHeight();
           int newWidth = 100;
           int newHeight = 100;
           // calculate the scale
           float scaleWidth = ((float) newWidth) / width;
           float scaleHeight = ((float) newHeight) / height;
           // create a matrix for the manipulation
           Matrix matrix = new Matrix();
           // resize the bit map
           matrix.postScale(scaleWidth, scaleHeight);
           Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
           width, height, matrix, true);
           // make a Drawable from Bitmap to allow to set the BitMap
           d = new BitmapDrawable(resizedBitmap);
       }

	   
	   return(d);
	   
   }

}